package cn.myjerry.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义的聚合类
 * @author Jerry
 *
 */
public class ConcreteMyAggregate {
	private List<Object> list = new ArrayList<>();
	public ConcreteMyAggregate() {
		super();
	}
	public ConcreteMyAggregate(List<Object> list) {
		super();
		this.list = list;
	}
	public void addObject(Object obj) {
		list.add(obj);
	}
	public void removeObject(Object obj) {
		list.remove(obj);
	}
	public List<Object> getList() {
		return list;
	}
	public void setList(List<Object> list) {
		this.list = list;
	}
	public MyIterator createIterator() {
		return new ConcreteIterator();
	}
	private class ConcreteIterator implements MyIterator {

		private int cursor;	// 定义游标
		
		@Override
		public void first() {
			this.cursor = 0;
		}

		@Override
		public void next() {
			if(cursor < list.size()) {
				cursor++;
			}
		}

		@Override
		public boolean hasNext() {
			return cursor<list.size() ? true : false;
		}

		@Override
		public boolean isFirst() {
			return cursor == 0;
		}

		@Override
		public boolean isLast() {
			return cursor == (list.size()-1);
		}

		@Override
		public Object getCurrentObj() {
			return list.get(cursor);
		}
		
	} 
}
